package com.operation.service.impl;

import com.operation.beans.Result;

import com.operation.beans.State;
import com.operation.beans.WorkOrderAllInfo;
import com.operation.dao.HandleInfoMapper;
import com.operation.dao.UserInfoMapper;
import com.operation.dao.WorkOrderMapper;
import com.operation.entity.HandleInfo;
import com.operation.entity.WorkOrder;
import com.operation.service.ManagerService;
import com.operation.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LCJ on 2017/10/24.
 */
@Service
public class ManagerServiceImpl extends State implements ManagerService {
    @Autowired
    WorkOrderMapper workOrderMapper;
    @Autowired
    HandleInfoMapper handleInfoMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public Result queryScheduleOfWorkOrders(WorkOrder workOrder) {
        List<WorkOrder> result = null;
        if (workOrder.getIsDistribution() != null) {
            result = workOrderMapper.findWorkOrderByDistribution(workOrder);
        } else if (workOrder.getIsReceive() != null) {
            result = workOrderMapper.findWorkOrderByIsReceive(workOrder);
        } else if (workOrder.getIscomment() != null) {
            result = workOrderMapper.findWorkOrderByIsComment(workOrder);
        } else if (workOrder.getIsPigeonhole() != null) {
//            result = workOrderMapper.findWorkOrderByIsPigeonhole(workOrder);
        } else if (workOrder.getIsComplete() != null) {
            result = workOrderMapper.findWorkOrderByIsComplete(workOrder);
        }
        return ResultUtil.success( result,0);
    }

    @Override
    public Result distributeWorkOrder(WorkOrder workOrder) {
        String myState = null;
        //分配时间
        workOrder.setDistributionTime(new Date());
        workOrder.setIsDistribution(1);
        workOrder.setIsReceive(1);
        workOrder.setIsComplete(0);
        workOrder.setIscomment(0);
        //a点
        int i = workOrderMapper.refreshDistribution(workOrder);
        //向状态表中插入一条数据
        HandleInfo handleInfo = new HandleInfo();
        handleInfo.setJobnumber(workOrder.getJobnumber());
        handleInfo.setReceiveOne(workOrder.getReceiveOne());
        handleInfo.setReason("自动分配！");
        handleInfo.setStatus(0);
        //b点
        int j = handleInfoMapper.insert(handleInfo);
        if (i == 1 && j == 1){
            myState =s9;
        }else if (i == -1 && j == 1){
            myState =s10;
        }else if (i == 1 && j == -1 ){
            myState =s11;
        }else if (i == -1 && j == -1){
            myState = s12;
        }
        return ResultUtil.success(myState,0);
    }

    @Override
    public Result endWorkOrder(WorkOrder workOrder) {
        String tip = null;
        WorkOrder temp = workOrderMapper.isComment(workOrder.getJobnumber());
        try{
            if (temp.getIscomment() == 1){
                workOrder.setIsPigeonhole(1);
                workOrder.setPigeonholeTime(new Date());
                workOrderMapper.updatePigeonhole(workOrder);
            }
        }catch(Exception e){
            tip = "用户未评论！";

        }
        return ResultUtil.success(tip,1);
    }

    @Override
    public List<WorkOrderAllInfo> checkComplaintList() {
        return workOrderMapper.checkComplaintList();
    }
}
